import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import '../common/global.dart';
import '../services/auth_service.dart';
import '../l10n/app_localizations.dart';

class UserSettingsPage extends StatelessWidget {
  const UserSettingsPage({super.key});

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);
    final AuthService authService = Get.find();

    return Scaffold(
      backgroundColor: const Color(0xFF0A0E27),
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [const Color(0xFF1A1F3A), const Color(0xFF0A0E27)],
          ),
        ),
        child: SafeArea(
          child: Column(
            children: [
              // 头部导航栏
              Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
                child: Row(
                  children: [
                    GestureDetector(
                      onTap: () => Get.back(),
                      child: SizedBox(
                        width: 40.w,
                        height: 40.h,
                        child: Icon(
                          Icons.arrow_back_ios,
                          color: Colors.white,
                          size: 20.sp,
                        ),
                      ),
                    ),
                    Expanded(
                      child: Text(
                        l10n.personalInformation,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 18.sp,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    SizedBox(width: 40.w), // 占位符，保持标题居中
                  ],
                ),
              ),

              Expanded(
                child: SingleChildScrollView(
                  padding: EdgeInsets.symmetric(horizontal: 20.w),
                  child: Column(
                    children: [
                      SizedBox(height: 40.h),

                      // 用户头像和信息
                      Obx(() {
                        final user = Global.user.value;
                        return Column(
                          children: [
                            // 头像
                            Container(
                              width: 120.w,
                              height: 120.w,
                              decoration: BoxDecoration(
                                color: const Color(0xFF0084FF).withOpacity(0.2),
                                shape: BoxShape.circle,
                                border: Border.all(
                                  color: const Color(0xFF00FFFF),
                                  width: 2,
                                ),
                                boxShadow: [
                                  BoxShadow(
                                    color: const Color(
                                      0xFF05FFFD,
                                    ).withOpacity(0.3),
                                    blurRadius: 5,
                                    spreadRadius: 1,
                                  ),
                                ],
                              ),
                              child: Center(
                                child: Image.asset(
                                  'assets/images/pic_home_top.png',
                                  width: 80.w,
                                  height: 80.w,
                                  fit: BoxFit.cover,
                                ),
                              ),
                            ),

                            SizedBox(height: 20.h),

                            Text(
                              '# ID:${Global.user.value?.account ?? ''}',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 22.sp,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                            SizedBox(height: 7.h),
                            Image.asset(
                              _getVipLevelRes(
                                Global.user.value?.user_info?.vip_level ?? '0',
                              ),
                              height: 30.h,
                              fit: BoxFit.cover,
                            ),
                          ],
                        );
                      }),

                      SizedBox(height: 60.h),

                      // 账号绑定区域
                      _buildAccountBindingSection(authService, l10n),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildAccountBindingSection(
    AuthService authService,
    AppLocalizations l10n,
  ) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // Google 账号绑定
        // _buildAccountBindingCard(
        //   icon: Icons.g_mobiledata,
        //   iconColor: Colors.white,
        //   title: l10n.googleAccount,
        //   subtitle: 'link google account',
        //   isConnected: authService.isGoogleConnected.value,
        //   onTap: () {
        //     if (authService.isGoogleConnected.value) {
        //       _showUnbindDialog('Google', l10n);
        //     } else {
        //       authService.signInWithGoogle();
        //     }
        //   },
        // ),
        SizedBox(height: 20.h),

        // Apple 账号绑定
        // _buildAccountBindingCard(
        //   icon: Icons.apple,
        //   iconColor: Colors.black,
        //   title: l10n.appleAccount,
        //   subtitle: 'link apple account',
        //   isConnected: authService.isAppleConnected.value,
        //   onTap: () {
        //     if (authService.isAppleConnected.value) {
        //       _showUnbindDialog('Apple', l10n);
        //     } else {
        //       authService.signInWithApple();
        //     }
        //   },
        // ),
        Obx(() {
          if (Global.user.value?.apple_user == null) {
            return SignInWithAppleButton(
              onPressed: () {
                authService.signInWithApple();
              },
            );
          } else {
            return _buildAccountBindingCard(
              icon: Icons.apple,
              iconColor: Colors.black,
              title: l10n.appleAccount,
              subtitle: Global.user.value?.apple_user?.email ?? '',
              isConnected: true,
              onTap: () {
                // if (authService.isAppleConnected.value) {
                //   _showUnbindDialog('Apple', l10n);
                // } else {
                //   authService.signInWithApple();
                // }
              },
            );
          }
        }),
      ],
    );
  }

  Widget _buildAccountBindingCard({
    required IconData icon,
    required Color iconColor,
    required String title,
    required String subtitle,
    required bool isConnected,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: double.infinity,
        height: 56.h,
        padding: EdgeInsets.symmetric(horizontal: 10.w),
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              const Color(0xFF0084FF).withOpacity(0.4),
              const Color(0xFF0084FF),
            ],
          ),
          borderRadius: BorderRadius.circular(60.r),
          border: Border.all(color: const Color(0xFF05FFFD), width: 2.w),
          boxShadow: [
            BoxShadow(
              color: const Color(0xFF00FFFF).withOpacity(0.2),
              blurRadius: 10,
              spreadRadius: 2,
            ),
          ],
        ),
        child: Row(
          children: [
            // 图标
            Icon(icon, color: iconColor, size: 32.sp),

            SizedBox(width: 16.w),

            Text(
              title,
              style: TextStyle(
                color: Colors.white,
                fontSize: 16.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
            SizedBox(height: 4.h),
            Expanded(
              child: Text(
                textAlign: TextAlign.end,
                isConnected ? subtitle : 'link $title account',
                style: TextStyle(
                  color: Color(0xff05FFFD).withValues(alpha: 0.6),
                  fontSize: 14.sp,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _showUnbindDialog(String accountType, AppLocalizations l10n) {
    Get.dialog(
      AlertDialog(
        backgroundColor: const Color(0xFF1A1F3A),
        title: Text(
          '${l10n.unbindAccount} $accountType',
          style: TextStyle(
            color: Colors.white,
            fontSize: 18.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
        content: Text(
          l10n.unbindConfirmMessage,
          style: TextStyle(
            color: Colors.white.withOpacity(0.8),
            fontSize: 14.sp,
          ),
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text(
              l10n.cancel,
              style: TextStyle(
                color: Colors.white.withOpacity(0.7),
                fontSize: 14.sp,
              ),
            ),
          ),
          TextButton(
            onPressed: () {
              Get.back();
              // TODO: 实现解绑逻辑
              Get.snackbar(
                l10n.unbindSuccess,
                '$accountType ${l10n.unbindSuccess}',
                snackPosition: SnackPosition.BOTTOM,
                backgroundColor: const Color(0xFF00FFFF).withOpacity(0.2),
                colorText: Colors.white,
              );
            },
            child: Text(
              l10n.unbind,
              style: TextStyle(
                color: const Color(0xFFFF3C22),
                fontSize: 14.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
        ],
      ),
    );
  }

  String _getVipLevelRes(String level) {
    print('getVipLevelRes: $level');
    switch (level) {
      case '0':
        return 'assets/images/icon_vip_normal.png';
      case '1':
        return 'assets/images/icon_vip_basic.png';
      case '2':
        return 'assets/images/icon_vip_moderate.png';
      case '3':
        return 'assets/images/icon_vip_advanced.png';
      case '4':
        return 'assets/images/icon_vip_top.png';
      default:
        return '';
    }
  }
}
